<template>
  <div>
    <!-- <template>
      <el-table
        :data="productList"
        style="width: 100%">
        <el-table-column
          label="ID"
          prop="id"
          width="100">
        </el-table-column>
        <el-table-column
          label="商品名称"
          prop="title"
          width="100">
        </el-table-column>
        <el-table-column
          label="商品描述"
          prop="content"
          width="100">
        </el-table-column>
        <el-table-column
          label="原价"
          prop="bprice"
          width="100">
        </el-table-column>
        <el-table-column
          label="现价"
          prop="price"
          width="150">
        </el-table-column>
        <el-table-column
          label="浏览量"
          prop="count"
          width="100">
        </el-table-column>
        <el-table-column
          label="库存"
          prop="stock"
          width="100">
        </el-table-column>

        <el-table-column
          prop="createTime"
          label="发布日期">
        </el-table-column>
        <el-table-column
          align="right">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="primary"
              @click="handleLook(scope.row)">查看
            </el-button>
            <el-button
              size="mini"
              @click="handleLike(scope.row)">收藏
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </template> -->
    <el-row>
      <el-col :span="6" v-for="(o) in productList" :key="o.id">
        <el-card :body-style="{ padding: '0px' }">
          <img :src="o.imgUrl" class="image">
          <div style="padding: 6px;">
            <p>标题：{{o.title}}</p>
            <p>描述：{{o.content}}</p>
            <p>原价：{{o.bprice}}</p>
            <p>现价：{{o.price}}</p>
            <p>库存：{{o.stock}}</p>
            <p>浏览量：{{o.count}}</p>
            <p>发布时间：{{o.createTime}}</p>
            <div class="bottom clearfix">
                <el-button type="primary" class="button" @click="handleLook(o)">查看商品</el-button>
                <el-button type="success" class="button" @click="handleLike(o)">收藏</el-button>
            </div>
          </div>
        </el-card>
      </el-col>
    </el-row>

    <!--  分页显示  -->
    <div :class="pageClass">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-sizes="[5,10,20]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
      </el-pagination>
    </div>

    <!--  弹框  -->
    <el-dialog
      title="查看商品"
      :visible.sync="dialogVisible"
      width="30%">
      <p>ID: {{ productInfo.id }}</p>
      <p>标题: {{ productInfo.title }}</p>
      <p>描述: {{ productInfo.content }}</p>
      <p>原价: {{ productInfo.bprice }}</p>
      <p>现价: {{ productInfo.price }}</p>
      <p>浏览量: {{ productInfo.count }}</p>
      <p>库存: {{ productInfo.stock }}</p>
      <p>发布日期: {{ productInfo.createTime }}</p>
      <span slot="footer" class="dialog-footer">
        <el-button @click="handleClose">取 消</el-button> &nbsp;&nbsp;
        <el-button type="primary" @click="handleClose">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: 'CardLike',
  data() {
    return {
      user: this.$store.getters.getUser, // 获取了登录人的信息
      msg: '商品列表',
      pageClass: "myPage",
      pageSize: 10,
      currentPage: 1, //默认显示的第几页,真实是数据是后端返回的
      total: 10, // 总共多少条数据,真实是数据是后端返回的
      dialogVisible: false,
      productList: [],
      productInfo: {
        id: 0,
        title: '',
        content: '',
        createTime: '',
        stock: 0,
        count: 0,
        bprice: 0,
        price: 0
      }
    }
  },
  methods: {
    handleLook(row) {
      let vm = this;
      this.axios({
        url: `http://localhost:9090/product/view/${row.id}`,
        method: 'GET'
      }).then(function (res) {
        vm.productInfo = res.data.data;
      });
      this.dialogVisible = true;
    },
    handleClose() {
      // 对话框关闭的时候 重新查询了商品列表
      this.getproductList(this.currentPage, this.pageSize);
      this.dialogVisible = false;
    },
    handleLike(row) {
      let vm = this;
      if(vm.user==null){
         vm.$message({
            message: "未登录，请先进行登录",
            type: 'error'
          });
        setTimeout(function () {
            vm.$router.push("/")
          }, 1000);
      }
      this.axios({
        url: `http://localhost:9090/favorites/like?userId=${vm.user.id}&productId=${row.id}`,
        method: 'GET'
      }).then(function (resp) {
        console.log(resp.data.data);
        if(resp.data.code==200){
          vm.$message({
            message: resp.data.msg,
            type: 'success'
          });
        }else{
          vm.$message({
            message: resp.data.msg,
            type: 'error'
          });
        }
        vm.getproductList(vm.currentPage, vm.pageSize);
      });
    },
    // 每页显示几条数据
    handleSizeChange(pageSize) {
      // 查询用户列表
      this.getproductList(this.currentPage, pageSize);
    },
    // 切换第几页
    handleCurrentChange(pageIndex) {
      // 查询用户列表
      this.getproductList(pageIndex, this.pageSize);
    },
    getproductList(pageIndex, pageSize) {
      // 发送axios请求
      let vm = this;
      this.axios({
        url: `http://localhost:9090/product/list?index=${pageIndex}&size=${pageSize}`,
        method: 'GET'
      }).then(function (res) {
        // 获取名片列表
        let list = res.data.data.list;
        vm.productList = list;
        vm.total = res.data.data.total;
        vm.pageSize = res.data.data.pageSize;
        vm.currentPage = res.data.data.pageNum;
      });
    }
  },
  mounted() {
    this.getproductList(this.currentPage, this.pageSize);
  }
}
</script>

<style scoped>
.myPage {
  margin-top: 40px;
  text-align: center;
}
.time {
    font-size: 13px;
    color: #999;
  }
  
  .bottom {
    margin-top: 13px;
    line-height: 12px;
  }

  .button {
    padding: 4;
    float: right;
  }

  .image {
    width: 100%;
    display: block;
  }

  .clearfix:before,
  .clearfix:after {
      display: table;
      content: "";
  }
  
  .clearfix:after {
      clear: both
  }
</style>
